package com.xiaodu.acer.phoneplay.presenter;

import com.xiaodu.acer.phoneplay.bean.PageBean;
import com.xiaodu.acer.phoneplay.bean.Subject;
import com.xiaodu.acer.phoneplay.bean.SubjectDetail;
import com.xiaodu.acer.phoneplay.common.rx.RxHttpReponseCompat;
import com.xiaodu.acer.phoneplay.common.rx.subscriber.ErrorHandlerSubscriber;
import com.xiaodu.acer.phoneplay.common.rx.subscriber.ProgressSubcriber;
import com.xiaodu.acer.phoneplay.presenter.contract.SubjectContract;
import com.xiaodu.acer.phoneplay.ui.base.BasePresenter;

import javax.inject.Inject;

import io.reactivex.Observer;

/**
 * @author 小杜 QQ:1392229149
 * @name ShouJiZhuShou
 * @class name：com.xiaodu.acer.phoneplay.presenter
 * @class describe
 * @time 2018/1/14 0:28
 * @change
 * @chang time
 * @class describe
 */
public class SubjectPresenter extends BasePresenter<SubjectContract.Model, SubjectContract.View> {

    @Inject
    public SubjectPresenter(SubjectContract.Model model, SubjectContract.View view) {
        super(model, view);
    }


    public void getSubjects(int page) {


        Observer subscriber = null;

        if (page == 0) {

            subscriber = new ProgressSubcriber<PageBean<Subject>>(mContext, mView) {
                @Override
                public void onNext(PageBean<Subject> pageBean) {
                    mView.showSubjects(pageBean);
                }
            };
        } else {
            subscriber = new ErrorHandlerSubscriber<PageBean<Subject>>(mContext) {
                @Override
                public void onComplete() {
                    mView.onLoadMoreComplete();
                }

                @Override
                public void onNext(PageBean<Subject> pageBean) {

                    mView.showSubjects(pageBean);
                }
            };
        }

        mModel.getSubjects(page)
                .compose( RxHttpReponseCompat.<PageBean<Subject>>compatResult())
                .subscribe(subscriber);


    }


    public void getSubjectDetail(int id) {


        mModel.getSubjectDetail(id).compose(RxHttpReponseCompat.<SubjectDetail>compatResult())
                .subscribe(new ProgressSubcriber<SubjectDetail>(mContext, mView) {
                    @Override
                    public void onNext(SubjectDetail subjectDetail) {

                        mView.showSubjectDetail(subjectDetail);
                    }
                });


    }


}
